package com.example.widget.view

import android.os.Bundle
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumnFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelProvider
import com.example.base.BaseActivity
import com.example.base.commonView.CommonCard
import com.example.base.commonView.CommonText
import com.example.base.commonView.CommonThemeNew
import com.example.base.commonView.TitleBar
import com.example.base.ui.black
import com.example.base.ui.darkGray
import com.example.base.ui.gray
import com.example.base.ui.white

class LiveDataActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewModel = ViewModelProvider(this).get(LiveDataViewModel::class.java)
        setContent {
            showView(viewModel.data)
        }

        viewModel.getData()
    }

    @Composable
    fun showView(data: MutableLiveData<List<String>>) {
        val context = ContextAmbient.current as LiveDataActivity
        val dataList by data.observeAsState(initial = emptyList())
        CommonThemeNew {
            TitleBar(title = "LiveData") {
                context.finish()
            }

            LazyColumnFor(items = dataList, modifier = Modifier.fillMaxSize()) { item ->
                //CardView
                CommonCard(backgroundColor = if (isSystemInDarkTheme()) darkGray else gray)
                {
                    //横向布局
                    Row(
                        //设置竖向居中
                        verticalAlignment = Alignment.CenterVertically,
                        //设置中间填充
                        horizontalArrangement = Arrangement.SpaceBetween,
                        //设置尺寸属性，并设置点击事件
                        modifier = Modifier.fillParentMaxWidth().padding(16.dp)
                    ) {
                        CommonText(
                            color = if (isSystemInDarkTheme()) white else black,
                            text = item,
                            fontSize = 16.sp,
                        )
                    }
                }
            }
        }
    }
}